package com.epam.edu.task02.Strings;

import com.epam.edu.task02.Strings.textParts.Text;

public class App {
	private static WorkWithFile workWithFile = new WorkWithFile();

	private static int getNumberSimilarWordsIntoText(String string) {
		Text text = new Text(string);
		int index = -1, maxcount = 0;
		for (int i = 0; i < text.getNumberofsentences(); i++) {
			int count = text.getSentence(i).getNumberSimilarWords();
			if (count >= maxcount) {
				maxcount = count;
				index = i;
			}
		}
		return index;
	}

	public static void main(String[] args) {
		String text = WorkWithFile.readFromFile();
		Text myText = new Text(text);
		StringBuilder textForPrint = new StringBuilder();
		textForPrint.append("Source: \r\n" + myText.toString()).append(
				"\r\n\r\n");

		textForPrint.append(swapFirstToLastWordsIntoText(text)).append(
				"\r\n\r\n");

		int index = getNumberSimilarWordsIntoText(text);
		textForPrint.append(
				"Maximum repetition of the word in this sentence:\r\n"
						+ myText.getSentence(index).toString()).append(
				"\r\n\r\n");

		textForPrint.append(sortByDescendingNumberOfWordsIntoText(text))
				.append("\r\n");
		WorkWithFile.writeIntoFile(textForPrint.toString());
	}

	private static String sortByDescendingNumberOfWordsIntoText(String string) {
		Text sortByDescNumberWords = new Text(string);
		sortByDescNumberWords.sortByDescendingNumberOfWords();
		return "Sentences are sorted in descending order according to the number of words:\r\n"
				+ sortByDescNumberWords.toString();
	}

	private static String swapFirstToLastWordsIntoText(String string) {
		Text text = new Text(string);
		int sentencescount = text.getSentenceCount();
		for (int i = 0; i < sentencescount; i++) {
			text.getSentence(i).swapFirstToLast();
		}
		return "After replacing the first and last words:\r\n"
				+ text.toString();
	}

}
